package com.scrumtrek.trainings.tdd;

import java.util.HashSet;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: skashin
 * Date: 20.02.13
 * Time: 18:50
 * To change this template use File | Settings | File Templates.
 */
public class BookStoreService {

    private IStore store;

    public BookStoreService(IStore store) {
        this.store = store;
    }

    public int getTotalBooksAmount() {
        return this.store.getTotalItemsAmountByType(Book.class);
    }

    public Set<Author> getAuthors() {
        Set<Author> authors = new HashSet<Author>();
        for(Item item :store.getItemsByType(Book.class)){
            authors.add(((Book) item).getAuthor());
        }

        return authors;
    }

    public Set<String> getTitles() {
        Set<String> titles = new HashSet<String>();
        for(Item item :store.getItemsByType(Book.class)){
            titles.add(item.getTitle());
        }

        return titles;
    }

    public Set<Book> getBooksByAuthor(String authorName) {
        HashSet<Book> books = new HashSet<Book>();
        for(Item item :store.getItemsByType(Book.class)){
            Book book = (Book) item;
            if(book.getAuthor().getName().equals(authorName)){
                books.add(book);
            }
        }
        return books;
    }

    public Set<Book> getBooksByTitle(String titleName) {
        HashSet<Book> books = new HashSet<Book>();
        for(Item item :this.store.getItemsByTitle(titleName)){
            if(item instanceof Book){
                books.add((Book) item);
            }
        }
        return books;

    }

    public Set<String> getFormattedTitles() {
        return this.store.getFormattedTitlesByType(Book.class);
    }

    public void addBook(Book item) {
        this.store.addItem(item);
    }

    public void addItem(Item item){
        this.store.addItem(item);
    }

    public void sell(Book book){
        this.store.sell(book);
    }
}
